package com.fary.cloud.netflix.eureka.config;

import com.fary.beans.factory.annotation.Autowired;
import com.fary.boot.autoconfigure.condition.ConditionalOnClass;
import com.fary.boot.context.properties.EnableConfigurationProperties;
import com.fary.context.annotation.Configuration;
import com.fary.util.StringUtils;
import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.discovery.EurekaClient;

import javax.annotation.PostConstruct;

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnClass({ EurekaInstanceConfigBean.class, EurekaClient.class, ConfigServerProperties.class })
public class EurekaClientConfigServerAutoConfiguration {

	@Autowired(required = false)
	private EurekaInstanceConfig instance;

	@Autowired(required = false)
	private ConfigServerProperties server;

	@PostConstruct
	public void init() {
		if (this.instance == null || this.server == null) {
			return;
		}
		String prefix = this.server.getPrefix();
		if (StringUtils.hasText(prefix) && !StringUtils
				.hasText(this.instance.getMetadataMap().get("configPath"))) {
			this.instance.getMetadataMap().put("configPath", prefix);
		}
	}

}